import tkinter as tk
from tkinter import messagebox

class GomokuGame:
    def __init__(self, master):
        self.master = master
        self.board_size = 15
        self.cell_size = 40
        self.board = [['.' for _ in range(self.board_size)] for _ in range(self.board_size)]
        self.current_player = 'X'
        self.create_widgets()

    def create_widgets(self):
        self.canvas = tk.Canvas(self.master, width=self.board_size*self.cell_size, height=self.board_size*self.cell_size)
        self.canvas.pack()
        self.canvas.bind("<Button-1>", self.handle_click)

        for i in range(self.board_size+1):
            x0 = i * self.cell_size
            y0 = 0
            x1 = i * self.cell_size
            y1 = self.board_size * self.cell_size
            self.canvas.create_line(x0, y0, x1, y1)
            x0 = 0
            y0 = i * self.cell_size
            x1 = self.board_size * self.cell_size
            y1 = i * self.cell_size
            self.canvas.create_line(x0, y0, x1, y1)

    def handle_click(self, event):
        col = event.x // self.cell_size
        row = event.y // self.cell_size
        if self.board[row][col] == '.':
            self.board[row][col] = self.current_player
            self.draw_piece(row, col)
            if self.check_win(self.current_player):
                self.show_message(f"Player {self.current_player} wins!")
                self.master.quit()
            elif self.is_full():
                self.show_message("It's a draw!")
                self.master.quit()
            else:
                self.current_player = 'O' if self.current_player == 'X' else 'X'

    def draw_piece(self, row, col):
        x0 = col * self.cell_size + self.cell_size//2
        y0 = row * self.cell_size + self.cell_size//2
        color = 'black' if self.current_player == 'X' else 'white'
        self.canvas.create_oval(x0-15, y0-15, x0+15, y0+15, fill=color)

    def check_win(self, player):
        directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
        for i in range(self.board_size):
            for j in range(self.board_size):
                if self.board[i][j] == player:
                    for direction in directions:
                        win = True
                        for k in range(1, 5):
                            ni, nj = i+k*direction[0], j+k*direction[1]
                            if 0 <= ni < self.board_size and 0 <= nj < self.board_size and self.board[ni][nj] == player:
                                continue
                            win = False
                            break
                        if win:
                            return True
        return False

    def is_full(self):
        for row in self.board:
            if '.' in row:
                return False
        return True

    def show_message(self, message):
        tk.messagebox.showinfo("Game Over", message)

def main():
    root = tk.Tk()
    game = GomokuGame(root)
    root.mainloop()

if __name__ == "__main__":
    main()
